﻿using SdlDotNet.Graphics;
//using SdlDotNet.;
using System.Drawing;
using NielsRask.Mucus;
using NielsRask.K242.Core;
using NielsRask.K242.Rendering;
using System;

namespace RenderTest1
{
	class SelectionLayer : LayerBase
	{
		Surface surface;
		Size size;
		IsoEngine isoEngine;
		Asteroid asteroid;
		Surface selector;
		GuiLayer guiLayer;

		public SelectionLayer( Size size, Asteroid asteroid, GuiLayer guiLayer )
		{
			this.size = size;
			this.guiLayer = guiLayer;
			isoEngine = new IsoEngine( 24, 48, @"..\..\Data\48x96\MouseMap.gif", new Point( 200, 0 ) );
			this.asteroid = asteroid;
			selector = new Surface( @"..\..\Data\48x96\whitebox.bmp" );
			surface = new Surface( size );
			surface.Fill( Color.Black );
			surface.TransparentColor = Color.Black;
			surface.Transparent = true;
		}

		public override bool RegisterClick( SdlDotNet.Input.MouseButtonEventArgs e )
		{
			Console.WriteLine( "SelectionLayer registered a mouse click!" );

			Point mapPoint = isoEngine.Diamond_MouseMapper( SdlDotNet.Input.Mouse.MousePosition );
			mapPoint.Offset( -1, -1 );

			//Point mapPoint = new Point( 2, 2 );
			if ( mapPoint.X >= 0 && mapPoint.Y >= 0 && mapPoint.X < this.asteroid.Terrain.GetLength( 0 ) && mapPoint.Y < this.asteroid.Terrain.GetLength( 1 ) )
			{
				if ( this.asteroid.Terrain[mapPoint.X, mapPoint.Y] != TerrainType.Space )
				{
					Console.WriteLine( "click is on map, lets build something!" );
					var type = guiLayer.Selector.SelectedItem.Type;
					asteroid.Colony.Build( type, mapPoint, true );
				}
			}

			return false;
		}

		public override Surface RenderSurface()
		{
			//Console.WriteLine( "rendering sectionlayer, mouse @ " + SdlDotNet.Input.Mouse.MousePosition );

			//SdlDotNet.Input.Mouse.
			Point mapPoint = isoEngine.Diamond_MouseMapper( SdlDotNet.Input.Mouse.MousePosition );
			mapPoint.Offset( -1, -1 );

			//Point mapPoint = new Point( 2, 2 );
			if ( mapPoint.X >= 0 && mapPoint.Y >= 0 && mapPoint.X < this.asteroid.Terrain.GetLength( 0 ) && mapPoint.Y < this.asteroid.Terrain.GetLength( 1 ) )
			{
				if ( this.asteroid.Terrain[mapPoint.X, mapPoint.Y] != TerrainType.Space )
				{
					//Console.WriteLine( "mouse within terrain!" );
					surface.Fill( Color.Black );
					Point p = isoEngine.Diamond_TilePlotter( mapPoint );//SdlDotNet.Input.Mouse.MousePosition );
					surface.Blit( selector, p );
				}
			}
			return surface;
		}
	}
}
